﻿using SuperFx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Net;

namespace SuperMoney.TestApp
{
	public partial class MainForm : Form
	{
		string consumerKey = "d0f89841d14d19cf94a619a12d3d51f9";
		string consumerSecret = "6e0da7e0a81f1392e1a893a5619ae813";
		string token = "2d0facf5b1d24ecea4243c12450ed08f";
		string secret = "1da428082b8241349528ec32a099aeb0";

		public MainForm()
		{
			InitializeComponent();
		}

		private void btnOrderCallback_Click(object sender, EventArgs e)
		{
			string dtString = DateTime.Now.ToString("yyyyMMddhhmmss");
			string order = "youmi" + dtString;
			string app = "123123123";
			string ad = "广告" + dtString;
			string adid = dtString;
			string user = "iOS-MeiRiMoney";
			string device = "youmi-deviceid";
			string chn = "0";
			string price = "2";
			string points = "100";
			string time = ((int)(DateTime.Now - DateTime.Parse("1970-01-01")).TotalSeconds).ToString();
			string sig = "456456456";

			Dictionary<string, string> dictParams = new Dictionary<string, string>();
			dictParams.Add("order", order);
			dictParams.Add("app", app);
			dictParams.Add("ad", ad);
			dictParams.Add("adid", adid);
			dictParams.Add("user", user);
			dictParams.Add("device", device);
			dictParams.Add("chn", chn);
			dictParams.Add("price", price);
			dictParams.Add("points", points);
			dictParams.Add("time", time);
			dictParams.Add("sig", sig);

			dictParams = dictParams.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);

			string str = null;
			foreach (var kv in dictParams)
			{
				string kvStr = String.Format("{0}={1}", kv.Key, kv.Value);
				if (str == null)
				{
					str = kvStr;
				}
				else
				{
					str += kvStr;
				}
			}
			string dev_server_secret_ios = "406157efa33b24d1";
			str += dev_server_secret_ios;
			string sign = Md5Hash.Compute(str);
			dictParams.Add("sign", sign);

			string query = null;
			foreach (var kv in dictParams)
			{
				string kvStr = String.Format("{0}={1}", HttpUtility.UrlEncode(kv.Key), HttpUtility.UrlEncode(kv.Value));
				if (query == null)
				{
					query = kvStr;
				}
				else
				{
					query = query + "&" + kvStr;
				}
			}

			string url = "http://localhost:13749/youmi/ios?" + query;
			WebClient wc = new WebClient();
			wc.DownloadData(url);
		}

		private void btnLogin_Click(object sender, EventArgs e)
		{
			string deviceId = "iOS-MeiRiMoney";
			string url = "http://localhost:10088/MeiRiMoney/Login?DeviceId={0}&ConsumerKey={1}";
			url = String.Format(url, deviceId, consumerKey);
			OAuthBase oauth = new OAuthBase();
			string normalizedUrl, normalizedRequestParameters;
			var sign = oauth.GenerateSignature(new Uri(url), consumerKey, consumerSecret, null, null, "GET", oauth.GenerateTimeStamp(), oauth.GenerateNonce(), out normalizedUrl, out normalizedRequestParameters);
			url = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + sign;

			rtbOut.AppendText(HttpClient.GetHtml(url, "utf-8"));
		}

		private void btnGetIntegral_Click(object sender, EventArgs e)
		{
			string deviceId = "iOS-MeiRiMoney";
			string url = "http://localhost:10088/MeiRiMoney/IntegralBalance?DeviceId={0}";
			url = String.Format(url, deviceId);
			OAuthBase oauth = new OAuthBase();
			string normalizedUrl, normalizedRequestParameters;
			var sign = oauth.GenerateSignature(new Uri(url), consumerKey, consumerSecret, token, secret, "GET", oauth.GenerateTimeStamp(), oauth.GenerateNonce(), out normalizedUrl, out normalizedRequestParameters);
			url = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + sign;

			rtbOut.AppendText(HttpClient.GetHtml(url, "utf-8"));
		}

		private void btnTaskRecrod_Click(object sender, EventArgs e)
		{
			string deviceId = "iOS-MeiRiMoney";
			string url = "http://localhost:10088/MeiRiMoney/TaskRecords?DeviceId={0}&Count=10&Page=1";
			url = String.Format(url, deviceId);
			OAuthBase oauth = new OAuthBase();
			string normalizedUrl, normalizedRequestParameters;
			var sign = oauth.GenerateSignature(new Uri(url), consumerKey, consumerSecret, token, secret, "GET", oauth.GenerateTimeStamp(), oauth.GenerateNonce(), out normalizedUrl, out normalizedRequestParameters);
			url = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + sign;

			rtbOut.AppendText(HttpClient.GetHtml(url, "utf-8"));
		}

		private void btnCommodityList_Click(object sender, EventArgs e)
		{

		}
	}
}
